package controller.command {
	import com.facebook.graph.FacebookDesktop;
	
	import controller.signal.PhotosLoadedSignal;
	
	import model.PhotoModel;
	import model.UserModel;
	import model.vo.PhotoVO;
	
	import mx.controls.Alert;
	
	import org.robotlegs.mvcs.Command;
	
	public class FBGetUserPhotos extends Command {
		
		[Inject] public var photoModel:PhotoModel;
		[Inject] public var photosLoaded:PhotosLoadedSignal;
		[Inject] public var userModel:UserModel;
		
		public override function execute():void {
			commandMap.detain(this);
			FacebookDesktop.api("/me/photos", onResult,{limit:500});
		}
		
		private function onResult(result:Object, fail:Object):void {
			
			result.hasOwnProperty("ne");
			if(fail) {
				// Handle error
			}else{ 
				
				var photoVO:PhotoVO;
				var photo:Object;
				var image:Object;
				var tags:Array;
				var tag:Object;
				var results:Array = result as Array;
				var userString:String = userModel.firstName + " " + userModel.lastName;
				var photos:Vector.<PhotoVO> = photoModel.photos;
				
				trace(result)
				
				for each(photo in results) {
					photoVO = new PhotoVO();
					image = photo.images[0];
					photoVO.url = photo.source;
					photoVO.width = image.width;
					photoVO.height = image.height;
					tags = photo.tags.data as Array;
					for each(tag in tags) {
						if(tag.name == userString) {
							photoVO.x = tag.x;
							photoVO.y = tag.y;
						}
					}
					photos.push(photoVO);
				}
			}
			
			// need to get more photos.
			
			photosLoaded.dispatch();
			//commandMap.release(this);
		}
	}
}